package com.until;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import springfox.documentation.oas.annotations.EnableOpenApi;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component//组件
@EnableScheduling//开启定时任务
public class AlarmTask {
    private static final SimpleDateFormat myFormat=new SimpleDateFormat("HH:mm:ss");
    private static int onlineCounter=0;

    //间隔5秒完成一次，cron默认定时执行的表达式
    @Scheduled(cron="0/5 * * * * *")
    public void run() throws InterruptedException {
        Thread.sleep(6000);
        System.out.println(onlineCounter++);
        System.out.println("cron,当前时间"+myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName()+"=====>"+System.currentTimeMillis()/1000);
    }

    //有固定频率执行上一个任务开始后，5秒后再开始执行
    @Scheduled(fixedRate = 5000)
    public void run1() throws InterruptedException {
        Thread.sleep(6000);
        System.out.println("fixedRate,当前时间"+myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName()+"=====>"+System.currentTimeMillis()/1000);
    }

    //表示固定延迟时间执行，基于上一个任务完成后，5秒后再开始执行
    @Scheduled(fixedDelay = 5000)
    public void run2() throws InterruptedException {
        Thread.sleep(6000);
        System.out.println("fixedDelay,当前时间"+myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName()+"=====>"+System.currentTimeMillis()/1000);
    }

    @Scheduled(initialDelay = 2000,fixedDelay = 5000)
    public void run3() {
        System.out.println("initialDely,当前时间"+myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName()+"=====>"+System.currentTimeMillis()/1000);
        System.out.println("fixedDelay,当前时间"+myFormat.format(new Date()));
    }
}
